home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / KOOB / common / server / server.cs < prev   
Encoding:
Text File  |  2005-11-23  |  3.8 KB  |  149 lines

  1. //-----------------------------------------------------------------------------
  2. // Torque Game Engine 
  3. // Copyright (C) GarageGames.com, Inc.
  4. //-----------------------------------------------------------------------------
  5.  
  6. //-----------------------------------------------------------------------------
  7. //-----------------------------------------------------------------------------
  8.  
  9. function portInit(%port)
  10. {
  11.    %failCount = 0;
  12.    while(%failCount < 10 && !setNetPort(%port)) {
  13.       echo("Port init failed on port " @ %port @ " trying next port.");
  14.       %port++; %failCount++;
  15.    }
  16. }
  17.  
  18. function createServer(%serverType, %mission)
  19. {
  20.    if (%mission $= "") {
  21.       error("createServer: mission name unspecified");
  22.       return;
  23.    }
  24.  
  25.    destroyServer();
  26.  
  27.    //
  28.    $missionSequence = 0;
  29.    $Server::PlayerCount = 0;
  30.    $Server::ServerType = %serverType;
  31.  
  32.    // Setup for multi-player, the network must have been
  33.    // initialized before now.
  34.    if (%serverType $= "MultiPlayer") {
  35.       echo("Starting multiplayer mode");
  36.  
  37.       // Make sure the network port is set to the correct pref.
  38.       portInit($Pref::Server::Port);
  39.       allowConnections(true);
  40.  
  41.       if ($pref::Net::DisplayOnMaster !$= "Never" )
  42.          schedule(0,0,startHeartbeat);
  43.    }
  44.  
  45.    // Load the mission
  46.    $ServerGroup = new SimGroup(ServerGroup);
  47.    onServerCreated();
  48.    loadMission(%mission, true);
  49. }
  50.  
  51.  
  52. //-----------------------------------------------------------------------------
  53.  
  54. function destroyServer()
  55. {
  56.    $Server::ServerType = "";
  57.    allowConnections(false);
  58.    stopHeartbeat();
  59.    $missionRunning = false;
  60.    
  61.    // End any running mission
  62.    endMission();
  63.    onServerDestroyed();
  64.  
  65.    // Delete all the server objects
  66.    if (isObject(MissionGroup))
  67.       MissionGroup.delete();
  68.    if (isObject(MissionCleanup))
  69.       MissionCleanup.delete();
  70.    if (isObject($ServerGroup))
  71.       $ServerGroup.delete();
  72.  
  73.    // Delete all the connections:
  74.    while (ClientGroup.getCount())
  75.    {
  76.       %client = ClientGroup.getObject(0);
  77.       %client.delete();
  78.    }
  79.  
  80.    $Server::GuidList = "";
  81.  
  82.    // Delete all the data blocks...
  83.    deleteDataBlocks();
  84.    
  85.    // Save any server settings
  86.    echo( "Exporting server prefs..." );
  87.    export( "$Pref::Server::*", "~/prefs.cs", false );
  88.  
  89.    // Dump anything we're not using
  90.    purgeResources();
  91. }
  92.  
  93.  
  94. //--------------------------------------------------------------------------
  95.  
  96. function resetServerDefaults()
  97. {
  98.    echo( "Resetting server defaults..." );
  99.    
  100.    // Override server defaults with prefs:   
  101.    exec( "~/defaults.cs" );
  102.    exec( "~/prefs.cs" );
  103.  
  104.    loadMission( $Server::MissionFile );
  105. }
  106.  
  107.  
  108. //------------------------------------------------------------------------------
  109. // Guid list maintenance functions:
  110. function addToServerGuidList( %guid )
  111. {
  112.    %count = getFieldCount( $Server::GuidList );
  113.    for ( %i = 0; %i < %count; %i++ )
  114.    {
  115.       if ( getField( $Server::GuidList, %i ) == %guid )
  116.          return;
  117.    }
  118.  
  119.    $Server::GuidList = $Server::GuidList $= "" ? %guid : $Server::GuidList TAB %guid;
  120. }
  121.  
  122. function removeFromServerGuidList( %guid )
  123. {
  124.    %count = getFieldCount( $Server::GuidList );
  125.    for ( %i = 0; %i < %count; %i++ )
  126.    {
  127.       if ( getField( $Server::GuidList, %i ) == %guid )
  128.       {
  129.          $Server::GuidList = removeField( $Server::GuidList, %i );
  130.          return;
  131.       }
  132.    }
  133.  
  134.    // Huh, didn't find it.
  135. }
  136.  
  137.  
  138. //-----------------------------------------------------------------------------
  139.  
  140. function onServerInfoQuery()
  141. {
  142.    // When the server is queried for information, the value
  143.    // of this function is returned as the status field of
  144.    // the query packet.  This information is accessible as
  145.    // the ServerInfo::State variable.
  146.    return "Doing Ok";
  147. }
  148.  
  149.